package org.kabeja.math;

import org.kabeja.dxf.helpers.Point;
import org.kabeja.dxf.helpers.Vector;

/**
 * @author shutao
 * @since 2022/4/24 16:38
 */
public class TransformContext {
    private double[][] transformMatrix;
    private double rotationAngle = 0.0D;
    private double scaleX = 1.0D;
    private double scaleY = 1.0D;
    private double scaleZ = 1.0D;
    private Vector translation = new Vector(1.0D, 1.0D, 1.0D);

    public TransformContext() {
        this.transformMatrix = new double[][]{{1.0D, 0.0D, 0.0D, 0.0D}, {0.0D, 1.0D, 0.0D, 0.0D}, {0.0D, 0.0D, 1.0D, 0.0D}, {0.0D, 0.0D, 0.0D, 1.0D}};
    }

    public TransformContext(double[][] var1) throws IllegalArgumentException {
        if (var1.length != 4 && var1[0].length != 4) {
            throw new IllegalArgumentException("Not a valid tranformation matrix.");
        } else {
            this.transformMatrix = var1;
        }
    }

    public double getRotationZAxis() {
        return this.rotationAngle;
    }

    public void setRotationZAxis(double var1) {
        this.rotationAngle = var1;
    }

    public void setScale(double var1) {
        this.setScale(var1, var1, var1);
    }

    public void setScale(double var1, double var3, double var5) {
        this.scaleX = var1;
        this.scaleY = var3;
        this.scaleZ = var5;
        double[][] var7 = new double[][]{{var1, 0.0D, 0.0D, 0.0D}, {0.0D, var3, 0.0D, 0.0D}, {0.0D, 0.0D, var5, 0.0D}, {0.0D, 0.0D, 0.0D, 1.0D}};
        this.transformMatrix = MathUtils.multiplyMatrixByMatrix(this.transformMatrix, var7);
    }

    public double getScaleX() {
        return this.scaleX;
    }

    public void setScaleX(double var1) {
        this.setScale(var1, 1.0D, 1.0D);
    }

    public double getScaleY() {
        return this.scaleY;
    }

    public void setScaleY(double var1) {
        this.setScale(1.0D, var1, 1.0D);
    }

    public double getScaleZ() {
        return this.scaleZ;
    }

    public void setScaleZ(double var1) {
        this.setScale(1.0D, 1.0D, var1);
    }

    public double[][] getTransformMatrix() {
        return this.transformMatrix;
    }

    public void setTransformMatrix(double[][] var1) {
        this.transformMatrix = var1;
    }

    public Vector getTranslation() {
        return this.translation;
    }

    public void setTranslation(Vector var1) {
        this.translation = var1;
        double[][] var2 = new double[][]{{1.0D, 0.0D, 0.0D, var1.getX()}, {0.0D, 1.0D, 0.0D, var1.getY()}, {0.0D, 0.0D, 1.0D, var1.getZ()}, {0.0D, 0.0D, 0.0D, 1.0D}};
        this.transformMatrix = MathUtils.multiplyMatrixByMatrix(this.transformMatrix, var2);
    }

    public Point transform(Point var1) {
        double[] var2 = new double[]{var1.getX(), var1.getY(), var1.getZ(), 1.0D};
        var2 = MathUtils.multiplyMatrixByVector(this.transformMatrix, var2);
        Point var3 = new Point(var2[0], var2[1], var2[2]);
        return var3;
    }
}
